package com.appspot.inkisapi.server;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.appspot.inkisapi.entity.BlogPost;
import com.appspot.inkisapi.service.BlogPostService;

@Controller
final class BlogPostController {
	// @Autowired
	// BlogPostService bpService;

	@RequestMapping(value = "/posts/{id}", method = RequestMethod.GET)
	@ResponseBody
	public BlogPost get(@PathVariable("id") long id) {
		return BlogPostService.getBlogPost(id);
	}

	@RequestMapping(value = "/posts", method = RequestMethod.GET)
	@ResponseBody
	public List<BlogPost> getAll() {
		return BlogPostService.getAllBlogPosts();
	}

	@RequestMapping(value = "/posts", method = RequestMethod.POST)
	@ResponseBody
	public void add(@RequestBody BlogPost bp) {
		try {
			BlogPostService.addOrUpdateBlogPost(bp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/posts/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public void update(@PathVariable("id") long bpId, @RequestBody BlogPost bp) {
		try {
			BlogPostService.addOrUpdateBlogPost(bp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/posts/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void delete(@PathVariable("id") long id) {
		try {
			BlogPost bp = BlogPostService.getBlogPost(id);
			BlogPostService.deleteBlogPost(bp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
